﻿namespace NavigationSampleWinForms
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Reflection;
    using System.Windows.Forms;

    using NavigationSampleWinForms.Properties;
    using NavigationSampleWinForms.Views;

    using Smart.Navigation;
    using Smart.Navigation.Windows.Forms;

    /// <summary>
    /// 
    /// </summary>
    public partial class MainForm : Form, IFunctionControl
    {
        private readonly Navigator navigator;

        private readonly Dictionary<Keys, FunctionKey> enabledFunctions = new Dictionary<Keys, FunctionKey>();
        private readonly List<Button> functionButtons = new List<Button>();

        /// <summary>
        /// 
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            InitializeFunctionKeys();

            // ナビゲーター初期化
            navigator = new Navigator(new ControlViewProvider(ViewPanel));
            navigator.Forwarding += OnForwarding;
            navigator.Exited += OnExited;

            navigator.AutoRegister(Assembly.GetExecutingAssembly());

            // 最初の画面へ遷移
            Show();
            navigator.Forward(ViewId.Menu);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnForwarding(object sender, ViewForwardEventArgs e)
        {
            // アプリケーション固有設定
            TitleLabel.Text = Resources.ResourceManager.GetString("View_" + e.ViewId, CultureInfo.CurrentCulture);

            var view = navigator.CurrentView as IApplicationView;
            if (view != null)
            {
                view.SetFunctionControl(this);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnExited(object sender, ViewExitEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnButtonExit(object sender, EventArgs e)
        {
            var view = navigator.CurrentView as IApplicationView;
            if (view != null)
            {
                view.OnButtonExit();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void InitializeFunctionKeys()
        {
            Fn1Button.Tag = Keys.F1;
            Fn2Button.Tag = Keys.F2;
            Fn3Button.Tag = Keys.F3;
            Fn4Button.Tag = Keys.F4;
            Fn5Button.Tag = Keys.F5;
            Fn6Button.Tag = Keys.F6;
            Fn7Button.Tag = Keys.F7;
            Fn8Button.Tag = Keys.F8;
            Fn9Button.Tag = Keys.F9;
            Fn10Button.Tag = Keys.F10;
            Fn11Button.Tag = Keys.F11;
            Fn12Button.Tag = Keys.F12;
            functionButtons.Add(Fn1Button);
            functionButtons.Add(Fn2Button);
            functionButtons.Add(Fn3Button);
            functionButtons.Add(Fn4Button);
            functionButtons.Add(Fn5Button);
            functionButtons.Add(Fn6Button);
            functionButtons.Add(Fn7Button);
            functionButtons.Add(Fn8Button);
            functionButtons.Add(Fn9Button);
            functionButtons.Add(Fn10Button);
            functionButtons.Add(Fn11Button);
            functionButtons.Add(Fn12Button);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keys"></param>
        public void UpdateFunctionKeys(FunctionKey[] keys)
        {
            enabledFunctions.Clear();
            if (keys != null)
            {
                foreach (var key in keys)
                {
                    enabledFunctions.Add(key.Key, key);
                }
            }

            foreach (var button in functionButtons)
            {
                var keyData = (Keys)button.Tag;
                if (enabledFunctions.ContainsKey(keyData))
                {
                    var key = enabledFunctions[keyData];
                    button.Enabled = true;
                    button.Text = key.Display;
                }
                else
                {
                    button.Enabled = false;
                    button.Text = string.Empty;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnFunctionClick(object sender, EventArgs e)
        {
            var button = sender as Button;

            // ファンクションディスパッチ
            var view = navigator.CurrentView as IApplicationView;
            if ((view != null) && (button != null))
            {
                view.OnFunctionKey((Keys)button.Tag);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (enabledFunctions.ContainsKey(keyData))
            {
                // ファンクションディスパッチ
                var view = navigator.CurrentView as IApplicationView;
                if (view != null)
                {
                    view.OnFunctionKey(keyData);
                    return true;
                }
            }

            return base.ProcessDialogKey(keyData);
        }
    }
}